package yto.freemarker.springaop.protocol;

import java.io.IOException;
import java.io.OutputStream;

/**
 * 消息协议层（转换数据）
 * @author dongxing
 **/
public class Protocol {


    private static final String DOLLAR_BYTE = "$";
    private static final String ASTERISK_BYTE = "*";
    private static final String STRING_BYTE = "\r\n";



    public static void sencommand(OutputStream outputStream,Commad commad,byte[]...args) {

        StringBuilder builder = new StringBuilder();
        builder.append(ASTERISK_BYTE).append(args.length + 1).append(STRING_BYTE);
        builder.append(DOLLAR_BYTE).append(commad.name().length()).append(STRING_BYTE);
        builder.append(commad).append(STRING_BYTE);
        for (byte[] arg : args) {
            builder.append(DOLLAR_BYTE).append(arg.length).append(STRING_BYTE);
            builder.append(new String(arg)).append(STRING_BYTE);
        }
        try {
            //写给服务端
            outputStream.write(builder.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public enum Commad {
        SET,GET
    }

}
